package cn.txm.org.service.impl;


import cn.txm.basic.service.impl.BaseServiceImpl;
import cn.txm.org.domain.Department;
import cn.txm.org.mapper.DepartmentMapper;
import cn.txm.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    DepartmentMapper departmentMapper;

    @Override  //父亲的add我用不了，那么我就自己写一个覆盖父亲的  （复写）
    public void add(Department department) {
        departmentMapper.save(department);
        this.update(department);
    }

    @Override  //父亲的update我用不了，那么我就自己写一个覆盖父亲的  （复写）
    public void update(Department department) {
        //如果新增的是顶级部门  直接拼接上自身ID就可以了
        if(department.getParent() == null){
            department.setDirPath("/" +department.getId());
        }else{
            ////先通过parentID查询出父亲的dirPath
            String pdirPath = departmentMapper.loadById(department.getParent().getId()).getDirPath();
            //在拼接上自己的id
            department.setDirPath(pdirPath +"/" +department.getId());
        }
        departmentMapper.update(department);
    }


    @Override
    public List<Department> getDeptTree() {
        return departmentMapper.getDeptTree();
    }
}
